package com.yurun.loginmodule.config;

import com.yurun.commonapi.pojo.Employee;
import com.yurun.commonapi.pojo.Permission;
import com.yurun.loginmodule.mapper.AdminMapper;
import com.yurun.loginmodule.mapper.PermissionMapper;
import com.yurun.loginmodule.mapper.RoleMapper;
import com.yurun.loginmodule.util.JWTUtil;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author 吼吼权
 * @version 1.0
 * @date 2021/1/27 18:06
 */
@Component
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private AdminMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    /**
     * 必须重写此方法，不然会报错
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JWTToken;
    }

    /**
     * 默认使用此方法进行用户名正确与否验证，错误抛出异常即可。
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("————身份认证方法11.24————");
        String token = (String) authenticationToken.getCredentials();
        System.out.println("这是认证的token"+token);
        // 解密获得username，用于和数据库进行对比
        String employeeId = JWTUtil.getUsername(token);
        System.out.println("认证"+employeeId);
        if (employeeId == null || !JWTUtil.verify(token, employeeId)) {
            throw new AuthenticationException("token认证失败！");
        }
        String role = JWTUtil.getRoleName(token);
        List<String> password = new ArrayList<>();
        if (role.equals("家长")) {
//            System.out.println(JWTUtil.getPhone(token));
            password = userMapper.getPPwd(BigInteger.valueOf(Long.parseLong(JWTUtil.getPhone(token))));
//            System.out.println(password);
        } else {
            password.add(userMapper.getPassword(BigInteger.valueOf(Long.parseLong(employeeId))));
        }

        if (password == null) {
            throw new AuthenticationException("该用户不存在！");
        }
        return new SimpleAuthenticationInfo(token, token, "MyRealm");
    }

    /**
     * 只有当需要检测用户权限的时候才会调用此方法，例如checkRole,checkPermission之类的
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("————权限认证————");
        String employeeId = JWTUtil.getUsername(principals.toString());
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //获得该用户角色
        String role = roleMapper.getRoleListByRoleId(BigInteger.valueOf(Long.parseLong(employeeId))).get(0);
        System.out.println(roleMapper.getRoleListByRoleId(BigInteger.valueOf(Long.parseLong(employeeId))));
        // 获取角色id
        int roleId = roleMapper.getRoleList(BigInteger.valueOf(Long.parseLong(employeeId))).get(0);
        // 获取权限列表id
        List<String> resourceId = permissionMapper.getPermissionName(roleId);
        //每个角色拥有默认的权限
        String rolePermission = permissionMapper.getPermission(employeeId);
        //每个用户可以设置新的权限
        //String permission = permissionMapper.getPermission(username);
        Set<String> roleSet = new HashSet<>();
        //需要将 role, permission 封装到 Set 作为 info.setRoles(), info.setStringPermissions() 的参数
        roleSet.add(role);
        // 将权限id插入set
        Set<String> permissionSet = new HashSet<>(resourceId);

        //permissionSet.add(permission);
        //设置该用户拥有的角色和权限
        info.setRoles(roleSet);
        info.setStringPermissions(permissionSet);
        System.out.println(permissionSet);
        return info;
    }
}
